﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodpackdetailServices : Repository<corecmsfoodpackdetail>, IcorecmsfoodpackdetailServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfoodpackdetail entity)
        {
            return base.Change<corecmsfoodpackdetail>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodpackdetail> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodpackdetail entity)
        {
            return base.Change<corecmsfoodpackdetail>().Update(a => new corecmsfoodpackdetail()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                foodId = entity.foodId,
                foodSpecId = entity.foodSpecId,
                packId = entity.packId,
                quantity = entity.quantity,
                setPrice = entity.setPrice,
                addPrice = entity.addPrice,
                cutPrice = entity.cutPrice,
                sort = entity.sort
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodpackdetail> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodpackdetail>().Update(a => new corecmsfoodpackdetail()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    foodId = item.foodId,
                    foodSpecId = item.foodSpecId,
                    packId = item.packId,
                    quantity = item.quantity,
                    setPrice = item.setPrice,
                    addPrice = item.addPrice,
                    cutPrice = item.cutPrice,
                    sort = item.sort
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodpackdetail>().Update(a => new corecmsfoodpackdetail()
            {
                isDelete = true
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodpackdetail>().Update(a => new corecmsfoodpackdetail()
                {
                    isDelete = true
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodpackdetail> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodpackdetail> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodpackdetail> lst = new List<corecmsfoodpackdetail>();
            var where = Expressionable.Create<corecmsfoodpackdetail>();
            where = where.And(a => a.isDelete == false);
            if (!string.IsNullOrEmpty(sName))
            {
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).ToList();
            }

            return lst;
        }
        #endregion

    }
}
